package com.ywwxhz.lib.kits;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.ywwxhz.MyApplication;
import com.ywwxhz.inter.HttpCallback;
import com.ywwxhz.lib.Configure;

/**
 * Created by ywwxhz on 2014/10/17.
 */
public class NetKit {

    private static NetKit instance = new NetKit();
    public static final String CONTENT_TYPE = "application/x-www-form-urlencoded; charset=UTF-8";

    private NetKit() {
    }


    public static int getConnectedType() {
        ConnectivityManager mConnectivityManager = (ConnectivityManager) MyApplication.getInstance()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
        if (mNetworkInfo != null && mNetworkInfo.isAvailable()) {
            return mNetworkInfo.getType();
        }
        return -1;
    }

    public static NetKit getInstance() {
        return instance;
    }

    public void getNewslistByPage(int page, String type, HttpCallback handlerInterface) {
        Map<String, String> params = new HashMap<>();
        params.put("type", type);
        params.put("page", page + "");
        params.put("_", System.currentTimeMillis() + "");
        HttpUtil.get(Configure.NEWS_LIST_URL, params, handlerInterface);
    }

    public void getNewslistByTopic(int page, String type, HttpCallback handlerInterface) {
        Map<String, String> params = new HashMap<>();
        params.put("id", type);
        params.put("page", page + "");
        params.put("_", System.currentTimeMillis() + "");
        HttpUtil.get(Configure.TOPIC_NEWS_LIST, params, handlerInterface);
    }

    public void getNewsBySid(String sid, HttpCallback handlerInterface) {
        HttpUtil.get(Configure.buildArticleUrl(sid), null, handlerInterface);
    }

    public void getNewsBySidSync(String sid, HttpCallback handlerInterface) {
        HttpUtil.get(Configure.buildArticleUrl(sid), null, handlerInterface);
    }

    public void getCommentBySnAndSid(String sn, String sid, HttpCallback handlerInterface) {
        Map<String, Object> params = new HashMap<>();
        params.put("op", "1," + sid + "," + sn);
        HttpUtil.postMulti(Configure.COMMENT_URL, params, handlerInterface);
    }

    public void setCommentAction(String op, String sid, String tid, String csrf_token, HttpCallback handlerInterface) {
        Map<String, Object> params = new HashMap<>();
        params.put("op", op);
        params.put("sid", sid);
        params.put("tid", tid);
        params.put("csrf_token", csrf_token);
        HttpUtil.postMulti(Configure.COMMENT_VIEW, params, handlerInterface);
    }

    public boolean isNetworkConnected() {
        ConnectivityManager cm = (ConnectivityManager) MyApplication.getInstance()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm != null) {
            //如果仅仅是用来判断网络连接
            //则可以使用 cm.getActiveNetworkInfo().isAvailable();
            NetworkInfo[] info = cm.getAllNetworkInfo();
            if (info != null) {
                for (NetworkInfo anInfo : info) {
                    if (anInfo.getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    public static boolean isWifiConnected() {
        ConnectivityManager cm = (ConnectivityManager) MyApplication.getInstance()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkINfo = cm.getActiveNetworkInfo();
        return networkINfo != null
                && networkINfo.getType() == ConnectivityManager.TYPE_WIFI;
    }

    public static boolean isMobileConnected() {
        ConnectivityManager cm = (ConnectivityManager) MyApplication.getInstance()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkINfo = cm.getActiveNetworkInfo();
        return networkINfo != null
                && networkINfo.getType() == ConnectivityManager.TYPE_MOBILE;
    }

    public static int getNetType(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        if (!(networkInfo != null && networkInfo.isConnected())) {
            return 0;
        }
        int type = networkInfo.getType();
        if (type == ConnectivityManager.TYPE_WIFI) {
            return 1;
        } else {
            return 2;
        }
    }
}
